package cn.huiyunche.bms.web.controller;

import cn.huiyunche.bms.backend.service.AreaService;
import cn.huiyunche.commons.domain.ResultDTO;
import cn.huiyunche.commons.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by felix on 2017/7/7 9527.
 */

@Controller
@RequestMapping("/barea")
public class AreaController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AreaController.class);

    @Autowired
    private AreaService areaService;


    /**
     * 根据code查询
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/by-code", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> queryByCode(String code) throws Exception {
        LOGGER.info("AreaController.queryByCode params : {}, {}", code);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        try {
            result.setData(areaService.queryByCode(code));
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AreaController.queryByCode error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     * 根据code查询
     * @param parentCode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/by-parentcode", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> queryByParentCode(String parentCode) throws Exception {
        LOGGER.info("AreaController.queryByParentCode params : {}, {}", parentCode);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        try {
            result.setData(areaService.queryByParentCode(parentCode));
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AreaController.queryByParentCode error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     * 查询省
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query-province", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> queryProvince() throws Exception {
        LOGGER.info("AreaController.queryProvince params : {}, {}");
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        try {
            result.setData(areaService.queryProvince());
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AreaController.queryProvince error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }
}
